﻿using DryIoc;
using ImTools;
using Prism.Commands;
using Prism.Common;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WheelClean2.Model;
using WheelClean2.Mould;

namespace WheelClean.ViewModels
{
    class TraceInfoViewModel : BindableBase, IDialogAware
    {

       

        public event Action<IDialogResult> RequestClose;

        private ObservableCollection<Track> _Infos;
        /// <summary>
        /// 主体列表
        /// </summary>
        public ObservableCollection<Track> Infos
        {
            get { return _Infos; }
            set { SetProperty(ref _Infos, value); }
        }


        private Track _Info;
        /// <summary>
        /// 当前主体
        /// </summary>
        public Track Info
        {
            get { return _Info; }
            set { SetProperty(ref _Info, value); }
        }
        private string _dataName;
        /// <summary>
        /// 参数1名称
        /// </summary>
        public string DataName
        {
            get { return _dataName; }
            set { SetProperty(ref _dataName, value); }
        }
        private string _dataSubText;
        /// <summary>
        /// 参数1后缀
        /// </summary>
        public string DataSubText
        {
            get { return _dataSubText; }
            set { SetProperty(ref _dataSubText, value); }
        }
        private bool _dataIsEnabled;
        /// <summary>
        /// 参数1是否可编辑
        /// </summary>
        public bool DataIsEnabled
        {
            get { return _dataIsEnabled; }
            set { SetProperty(ref _dataIsEnabled, value); }
        }
        private Visibility _dataVisibility1;
        /// <summary>
        /// 参数1轴目标位置可见
        /// </summary>
        public Visibility DataVisibility1
        {
            get { return _dataVisibility1; }
            set { SetProperty(ref _dataVisibility1, value); }
        }
        private Visibility _dataVisibility2;
        /// <summary>
        /// 参数1激光开关可见
        /// </summary>
        public Visibility DataVisibility2
        {
            get { return _dataVisibility2; }
            set { SetProperty(ref _dataVisibility2, value); }
        }

        private Visibility _dataVisibility3;
        /// <summary>
        /// 参数1激光工艺库可见
        /// </summary>
        public Visibility DataVisibility3
        {
            get { return _dataVisibility3; }
            set { SetProperty(ref _dataVisibility3, value); }
        }




        private string _dataName2;
        /// <summary>
        /// 参数2名称
        /// </summary>
        public string DataName2
        {
            get { return _dataName2; }
            set { SetProperty(ref _dataName2, value); }
        }
        private string _data2SubText;
        /// <summary>
        /// 参数2后缀
        /// </summary>
        public string Data2SubText
        {
            get { return _data2SubText; }
            set { SetProperty(ref _data2SubText, value); }
        }
        private bool _data2IsEnabled;
        /// <summary>
        /// 参数2是否可编辑
        /// </summary>
        public bool Data2IsEnabled
        {
            get { return _data2IsEnabled; }
            set { SetProperty(ref _data2IsEnabled, value); }
        }


        private List<CheckList> _functionList;
        /// <summary>
        /// 设备列表
        /// </summary>
        public List<CheckList> FunctionList
        {
            get { return _functionList; }
            set { SetProperty(ref _functionList, value); }
        }

        private List<CheckList> _boolList;
        /// <summary>
        /// 选择是否
        /// </summary>
        public List<CheckList> BoolList
        {
            get { return _boolList; }
            set { SetProperty(ref _boolList, value); }
        }

        private DataTable _db_info;
        /// <summary>
        /// 步列表
        /// </summary>
        public DataTable Db_info
        {
            get { return _db_info; }
            set { SetProperty(ref _db_info, value); }
        }

        private int _copyNumber;
        /// <summary>
        /// 要复制的步
        /// </summary>
        public int CopyNumber
        {
            get { return _copyNumber; }
            set { SetProperty(ref _copyNumber, value); }
        }


        




        public DelegateCommand DelegateCommand_Save { get; private set; }
        public DelegateCommand DelegateCommand_Copy { get; private set; }

        
        public DelegateCommand<int?> SelectionChangedCommand { get; private set; }


        private List<LaserInfo> LaserInfos;
        private AppDbContext context;

        public string Title => "轨迹编辑";

        TraceInfoViewModel()
        {
            //using (var db = new AppDbContext())
            //{
            //    var expensiveProducts = db.Products.ToList();
            //}
            context = new AppDbContext();
            //获取所有工艺
            LaserInfos = context.LaserInfos.ToList();
            //默认值
            Info = new Track();
            Info.FunctionCode = 1;
            SelectionChanged(Info.FunctionCode);
            SelectionChangedCommand = new DelegateCommand<int?>(SelectionChanged);
            DelegateCommand_Save = new DelegateCommand(Save);
            DelegateCommand_Copy = new DelegateCommand(Copy);





            //选择列表初始化
            FunctionList = Enum.GetValues(typeof(TraceEnum))
                .Cast<TraceEnum>()
                .Select(e => new CheckList()
                {
                    Name = e.ToString(),
                    Code = (int)e
                }).ToList();
            GetComboBoxData(1);




        }

        public void GetComboBoxData(int type)
        {
            if(type==1)
            {
                BoolList = new List<CheckList>();
                BoolList.Add(new CheckList() { Code = 1, Name = "开启" });
                BoolList.Add(new CheckList() { Code = 0, Name = "关闭" });
            }
            else
            {
                BoolList = new List<CheckList>();
                BoolList.AddRange(LaserInfos.Select(t => new CheckList() { Code = t.ID, Name = t.ListName }));
            

            }
        }

        private void Save()
        {
            DialogParameters parameters = new DialogParameters();
            parameters.Add("info", Info);
            RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
        }

        /// <summary>
        /// 功能码切换UI变换
        /// </summary>
        /// <param name="obj"></param>
        private void SelectionChanged(int? obj)
        {
            DataName = "";
            DataName2 = "";
            DataSubText = "";
            Data2SubText = "";
            DataIsEnabled = true;
            Data2IsEnabled = true;
            DataVisibility1 = Visibility.Collapsed;
            DataVisibility2 = Visibility.Collapsed;
            DataVisibility3 = Visibility.Collapsed;
            Info.FunctionName = Enum.GetName(typeof(TraceEnum), obj); 
            if (obj==1|| obj == 2|| obj == 3)
            {
                DataName = "轴目标位置";
                DataName2 = "轴速度";
                DataSubText = "mm";
                Data2SubText = "mm/s";
                DataVisibility1 = Visibility.Visible;
            }
            else if (obj == 4)
            {
                DataName = "激光开关";
                Data2IsEnabled = false;
            
                GetComboBoxData(1);
                DataVisibility2 = Visibility.Visible;
            }
            else if (obj == 5)
            {
                DataName = "激光参数工艺库调取编号";
                Data2IsEnabled = false;
                GetComboBoxData(2);
                DataVisibility2 = Visibility.Visible;
            }
            else if (obj == 6)
            {
                DataName2 = "转轮速度";
                Data2SubText = "mm/s";
                DataIsEnabled = false;
                DataVisibility3 = Visibility.Visible;

            }
            else if (obj == 7)
            {
                DataName2 = "轮对线速度系数";
                Data2SubText = "";
                DataIsEnabled = false;
                DataVisibility3 = Visibility.Visible;

            }
            else if (obj == 20)
            {
                DataIsEnabled = false;
                Data2IsEnabled = false;
            }

        }

        /// <summary>
        /// 引用已有步
        /// </summary>
        private void Copy()
        {

        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters.ContainsKey("info"))
            {
                var info= parameters.GetValue<Track>("info");
                if(info!=null)
                {
                    Info = info;
                    SelectionChanged(Info.FunctionCode);
                }

            }
            else if (parameters.ContainsKey("max"))
            {
                var max = parameters.GetValue<int>("max");
                Info.Number = max + 1;
            }

        }
    }
}
